package com.bootdo.crm.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.crm.domain.OwnerRecordDO;
import com.bootdo.crm.service.OwnerRecordService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 负责人变更记录表
 * 
 * @author cui
 * @email 1992lcg@163.com
 * @date 2020-02-20 13:14:28
 */
 
@Controller
@RequestMapping("/crm/ownerRecord")
public class OwnerRecordController {
	@Autowired
	private OwnerRecordService ownerRecordService;
	
	@GetMapping()
	@RequiresPermissions("crm:ownerRecord:ownerRecord")
	String OwnerRecord(){
	    return "crm/ownerRecord/ownerRecord";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("crm:ownerRecord:ownerRecord")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<OwnerRecordDO> ownerRecordList = ownerRecordService.list(query);
		int total = ownerRecordService.count(query);
		PageUtils pageUtils = new PageUtils(ownerRecordList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("crm:ownerRecord:add")
	String add(){
	    return "crm/ownerRecord/add";
	}

	@GetMapping("/edit/{recordId}")
	@RequiresPermissions("crm:ownerRecord:edit")
	String edit(@PathVariable("recordId") Integer recordId,Model model){
		OwnerRecordDO ownerRecord = ownerRecordService.get(recordId);
		model.addAttribute("ownerRecord", ownerRecord);
	    return "crm/ownerRecord/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("crm:ownerRecord:add")
	public R save( OwnerRecordDO ownerRecord){
		if(ownerRecordService.save(ownerRecord)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("crm:ownerRecord:edit")
	public R update( OwnerRecordDO ownerRecord){
		ownerRecordService.update(ownerRecord);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("crm:ownerRecord:remove")
	public R remove( Integer recordId){
		if(ownerRecordService.remove(recordId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("crm:ownerRecord:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] recordIds){
		ownerRecordService.batchRemove(recordIds);
		return R.ok();
	}
	
}
